#!/usr/bin/python

import mp4
from mp4 import EndOfStreamError

def tabify(str, tab):
    t = '\t' * tab
    return '\n'.join((t + s) for s in str.split('\n'))
        

def print_box(a, tab=0):
    print ''
    print tabify('box:', tab)
    print tabify(str(a.box_header), tab)
    box_type = a.optional_boxed_data.box_type
    if box_type:
        print tabify('box_type:', tab)
        print tabify(str(box_type), tab)
    if a.optional_boxed_data.sub_boxes:
        print tabify('sub boxes:', tab)
        for sb in  a.optional_boxed_data.sub_boxes:
            print_box(sb, tab+1)
    

def test(filename):
    f = open(filename, 'rb')
    
    while True:
        try:
            a = mp4.box.parse_stream(f)
        except EndOfStreamError:
            break
        print_box(a)
        
if __name__ == '__main__':
    import sys
    try:
        test(*sys.argv[1:])
    except Exception, e:
        import pdb
        import traceback
        sys.last_type, sys.last_value, sys.last_traceback = sys.exc_info()
        traceback.print_exc()
        pdb.pm()




















